var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form
    ,$ = layui.jquery
    ,laydate = layui.laydate;
layui.use(['tableEdit', 'form'], function () {
    let layer = layui.layer //弹层
        ,form = layui.form
        ,$ = layui.jquery

    lay('.starttime').each(function(){
        laydate.render({
            elem: this
            ,btns: ['clear', 'now']
            ,trigger: 'click'
        });
    });


    layer.ready(function () {
        let bankAccountList = parent.bankAccountList;
        for(let i in bankAccountList){
            $('#bankName').append(new Option(bankAccountList[i].name, bankAccountList[i].name));
            $('#account').append(new Option(bankAccountList[i].bankAccount, bankAccountList[i].bankAccount));
        }
        form.render('select');
    });


    form.on('submit(form-element)', function(data){
        //明细数据
        let oldData =  parent.table.cache["realpaysubsidiarytableid"];
        let setIdList = parent.setIdList;
        for(let i in oldData){
            for(let j in setIdList){
                if(oldData[i]["tempId"] === setIdList[j]){
                    //实付日期
                    let time = data.field.time;
                    if(!isEmpty(time)){
                        oldData[i]["time"] = time;
                    }

                    //付款银行
                    let bankName = data.field.bankName;
                    if(!isEmpty(bankName)){
                        oldData[i]["bankName"] = bankName;
                    }

                    //付款账号
                    let account = data.field.account;
                    if(!isEmpty(account)){
                        oldData[i]["account"] = account;
                    }
                }
            }
        }
        //重载表单
        parent.table.reload('realpaysubsidiarytableid', {data:oldData, limit:oldData.length});
        //执行接口
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则关闭此页面
            let index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },1000)
    });


    $("#shutdown").on("click",function () {
        let index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    })

    //字段是否为空或者零
    function isEmpty(obj){
        if(typeof obj == "undefined" || obj == null || obj == "" || obj == 0){
            return true;
        }else{
            return false;
        }
    }
})
